<template>
  <div>
    <ky-table
      :getData="getPackageList"
      :search_data="search_data"
      :search_criteria="search_criteria"
    >
      <template v-slot:table-action>
        <authorization-button class="kylin-item-button" @click="scanNow" name="scan:now">{{
          $t("system.scan_now")
        }}</authorization-button>
      </template>
      <template v-slot:table>
        <el-table-column prop="name" :label="$t('system.package_name')">
        </el-table-column>
        <el-table-column
          prop="versionRelease"
          :label="$t('system.package_version')"
        >
        </el-table-column>
        <el-table-column prop="installedAt" :label="$t('system.install_time')">
        </el-table-column>
      </template>
    </ky-table>
  </div>
</template>

<script>
import kyTable from "@/components/KyTable";
import { getPackageList } from "@/api/host_detail";
import { scanNow } from "@/api/scan";
import AuthorizationButton from "@/components/AuthorizationButton";
export default {
  components: {
    kyTable,
    AuthorizationButton
  },
  data() {
    return {
      search_criteria: {
        hostId: this.$route.params.id,
        sortBy: "id",
        sortOrder: "desc",
      },
      search_data: {
        name: {
          name: "system.package_name",
          type: "virtual_list",
          func: this.getPackageNames,
          items: []
        },
        versionRelease: {
          name: "system.package_version",
          type: "text",
        },
        installedAt: {
          name: "system.install_time",
          type: "date",
        },
      },
    };
  },
  methods: {
    getPackageList,
    getPackageNames() {
        getPackageList({paged: false, hostId: this.$route.params.id}).then(res => {
            let result = res.data.data.results;
            let items = []
            result.forEach(item => {
                items.push(item.name)
            })
            this.search_data.name.items = items;
        })
    },
    scanNow() {
      let ids = [this.$route.params.id]
      scanNow({ hostIds: ids }).then((res) => {
        if (res.data.code == "0") {
          this.$message({
            type: "success",
            message: res.data.message,
          });
        } else {
          this.$message({
            type: "error",
            message: res.data.message,
          });
        }
        this.$router.push({
            name: "log_detail",
            params: { id: res.data.data.id },
          });
      });
    },
  },
};
</script>

<style>
</style>